<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { UserVo } from '/@/api/openapi'
import { useUserFriendStore, useUserStore } from '/@/store'
import { storeToRefs } from 'pinia'
import { Refresh, Search } from '@element-plus/icons-vue'

const userFriendStore = useUserFriendStore()
const store = useUserStore()
const { userFriend } = storeToRefs(userFriendStore)
const loading = ref(false)
const searchKeyword = ref('')
const showUserInfo = ref(false)
const selectedUser = ref<UserVo | null>(null)

const loadData = async () => {
  loading.value = true
  try {
    await userFriendStore.updateUserFriend()
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  loadData()
}

const handleUserClick = (user: UserVo) => {
  selectedUser.value = user
  showUserInfo.value = true
}

onMounted(() => {
  if (store.isSign) {
    loadData()
  }
})
</script>

<template>
  <div class="friend-list-container">
    <div class="header">
      <h2 class="title">我的好友</h2>
      <div class="search-box">
        <el-input
          v-model="searchKeyword"
          placeholder="搜索好友"
          class="search-input"
          :prefix-icon="Search"
          @keyup.enter="handleSearch"
        />
        <el-button type="primary" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <div class="content">
      <el-button
        @click="loadData"
        class="refresh-btn"
        :loading="loading"
        type="primary"
        plain
      >
        <el-icon><Refresh /></el-icon>
        刷新列表
      </el-button>

      <div class="friend-grid" v-loading="loading">
        <div
          v-for="friend in userFriend"
          :key="friend.id"
          class="friend-card"
        >
          <div class="friend-card-left">
            <el-avatar
              :size="60"
              :src="friend.avatar"
              class="friend-avatar"
            ></el-avatar>
          </div>
          <div class="friend-card-right">
            <div class="friend-info">
              <h3 class="friend-name">{{ friend.username }}</h3>
              <p class="friend-signature">{{ friend.signature || '这个人很懒，什么都没写~' }}</p>
            </div>
            <div class="friend-contact">
              <p class="contact-item" v-if="friend.email">
                <i class="el-icon-message"></i>
                {{ friend.email }}
              </p>
              <p class="contact-item" v-if="friend.phone">
                <i class="el-icon-phone"></i>
                {{ friend.phone }}
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 用户详情弹窗 -->
    <el-dialog
      v-model="showUserInfo"
      title="用户信息"
      width="400px"
      :close-on-click-modal="false"
    >
      <div class="user-detail" v-if="selectedUser">
        <div class="user-header">
          <el-avatar
            :size="100"
            :src="selectedUser.avatar"
            class="detail-avatar"
          ></el-avatar>
          <h2>{{ selectedUser.username }}</h2>
        </div>
        <div class="user-info-list">
          <div class="info-item">
            <span class="label">个性签名：</span>
            <span>{{ selectedUser.signature || '这个人很懒，什么都没写~' }}</span>
          </div>
          <div class="info-item">
            <span class="label">邮箱：</span>
            <span>{{ selectedUser.email || '未设置' }}</span>
          </div>
          <div class="info-item">
            <span class="label">手机：</span>
            <span>{{ selectedUser.phone || '未设置' }}</span>
          </div>
        </div>
        <div class="action-buttons">
          <el-button type="primary" @click="showUserInfo = false">关闭</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.friend-list-container {
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  height: calc(100vh - 180px);
  display: flex;
  flex-direction: column;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;

  .title {
    margin: 0;
    color: #303133;
    font-size: 24px;
  }

  .search-box {
    display: flex;
    gap: 10px;

    .search-input {
      width: 300px;
    }
  }
}

.content {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;

  .refresh-btn {
    margin-bottom: 16px;
    align-self: flex-start;
  }
}

.friend-grid {
  display: flex;
  flex-direction: column;
  gap: 15px;
  padding: 10px;
  overflow-y: auto;
  flex: 1;
}

.friend-card {
  background: #fff;
  border-radius: 12px;
  padding: 15px;
  display: flex;
  gap: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid #ebeef5;
  align-items: flex-start;

  &:hover {
    transform: translateX(5px);
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    background-color: #f6f8fc;
  }

  .friend-card-left {
    flex-shrink: 0;
    
    .friend-avatar {
      border: 2px solid #fff;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }
  }

  .friend-card-right {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;

    .friend-info {
      .friend-name {
        margin: 0 0 5px;
        font-size: 16px;
        color: #303133;
        font-weight: 600;
      }

      .friend-signature {
        margin: 0;
        font-size: 14px;
        color: #606266;
        line-height: 1.4;
      }
    }

    .friend-contact {
      margin-top: 5px;
      
      .contact-item {
        margin: 0;
        font-size: 13px;
        color: #909399;
        display: flex;
        align-items: center;
        gap: 5px;
        
        i {
          font-size: 14px;
        }
      }
    }
  }
}

@media screen and (max-width: 768px) {
  .header {
    flex-direction: column;
    gap: 16px;

    .search-box {
      width: 100%;

      .search-input {
        width: 100%;
      }
    }
  }
}
</style>
